package com.base.cn.platform.os.controller.course.cou;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.course.CourseType;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.course.cou.Course;
import com.base.cn.platform.os.entity.course.cou.CourseCondition;
import com.base.cn.platform.os.entity.course.cou.buy.CourseBuyRecord;
import com.base.cn.platform.os.entity.course.count.CourseSumCount;
import com.base.cn.platform.os.service.course.cou.CourseBiz;
import com.base.cn.platform.os.service.course.cou.buy.CourseBuyRecordBiz;
import com.base.cn.platform.os.service.course.cou.packageCou.PackageCourseBiz;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.op.groom.OpGroomDataService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 课程Controller
 *
 * @author s.li
 * @create 2018-04-17-10:55
 */
@RestController
@RequestMapping("/course")
public class CourseController extends BaseController {
    @Autowired
    private CourseBiz courseBiz;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private PackageCourseBiz packageCourseBiz;
    @Autowired
    private OpGroomDataService opGroomDataService;
    @Autowired
    private CusUserService cusUserService;
    @Autowired
    private CourseBuyRecordBiz courseBuyRecordBiz;

    /**
     * 复制课程数据
     * @param courseId 课程ID
     * @param sysUserId　管理员ID
     */
    @RequestMapping("/copyCourse")
    public Map<String,Object> copyCourse(@RequestParam("courseId") BigDecimal courseId,
                                         @RequestParam("sysUserId") BigDecimal sysUserId){
        Course course = courseBiz.findById(courseId,null);
        if(ObjectUtils.isEmpty(course)){
            return ResultUtil.ERROR("课程不存在");
        }
        course.setCreateUserId(sysUserId);
        course = courseBiz.copyCourse(course);
        return ResultUtil.SUCCESS("课程复制成功",course);
    }

    /**
     * 获取购买课程的用户课程列表
     * @param courseId 课程ID
     * @param currentPage 页码
     * @param pageSize 查询数量
     * @return PageInfo<CourseBuyRecord>
     */
    @RequestMapping("/findCourseBuyUserList")
    public PageInfo<CourseBuyRecord> findCourseBuyUserList(@RequestParam("courseId") BigDecimal courseId,
                                                           @RequestParam("currentPage") int currentPage,
                                                           @RequestParam("pageSize") int pageSize){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        return courseBuyRecordBiz.findCourseBuyUserList(courseId,page);
    }

    /**
     * 课程数量
     * @return BigDecimal
     */
    @RequestMapping("/courseCount")
    public BigDecimal courseCount(@RequestBody CourseCondition condition){
        return courseBiz.getCourseCount(condition);
    }

    /**
     * 保存课程基本信息
     * @param course 课程对象
     * @return Map<String,Object>
     */
    @RequestMapping("/saveCourse")
    public Map<String,Object> saveCourse(@RequestBody Course course){
        if(StringUtils.isEmpty(course.getCourseName())){
            return ResultUtil.ERROR("请填写课程名称");
        }else{
            if(course.getCourseName().trim().length()>30){
                return ResultUtil.ERROR("课程名称限制在30个字以内");
            }
        }
        //不是套餐课程的判断
        if(!course.getCourseTypeKey().equals(CourseType.COURSE_TYPE_PACKAGE.getKey())){
            String message = StringUtils.checkSubjectCondition(course.getSubjectIds());
            if(message !=null){
                return ResultUtil.ERROR(message);
            }
        }
        if(course.getOrPrice()==null){
            return ResultUtil.ERROR("请填写课程原价");
        }
        if(course.getRealPrice()==null){//如果课程的实销价为空，则设置原价为实销价
            course.setRealPrice(0D);
        }else{
            if(course.getRealPrice().doubleValue() > course.getOrPrice().doubleValue()){
                return ResultUtil.ERROR("实销价不能大于课程原价");
            }
        }
        //获取售卖方式
        TreeMap<String,String> saleContextMap = sysConfigService.getSysConfigContext(ConfigName.SALE);
        String selaSwitch = saleContextMap.get("selaSwitch");
        if(selaSwitch.equals("3")){//组合销售方式
            if(course.getMemberPrice()==null){//如果会员价格为空，则设置实售价为会员价
                course.setMemberPrice(0D);
            }else{
                if(course.getMemberPrice().doubleValue() > course.getRealPrice().doubleValue()){
                    return ResultUtil.ERROR("会员价不能大于实销价");
                }
            }
        }
        if(course.getValidType().intValue()==1){//购买后指定天数内过期
            if(course.getValidDay()==null){
                return ResultUtil.ERROR("请填写购买后有效天数");
            }else{
                if(course.getValidDay().intValue()<=0){
                    return ResultUtil.ERROR("购买后有效天数必须大于0，且是整数");
                }
            }
        }else if (course.getValidType().intValue() == 2) {//指定日期前过期
            if (course.getValidTime() == null) {
                return ResultUtil.ERROR("请选择购买课程后的到期时间");
            }
            if (ObjectUtils.isEmpty(course.getDontBuyTime())) {
                return ResultUtil.ERROR("请选择截止售卖日期");
            }
            if (ObjectUtils.isNotEmpty(course.getValidTime()) && ObjectUtils.isNotEmpty(course.getDontBuyTime())) {
                if (course.getDontBuyTime().getTime() > course.getValidTime().getTime()) {
                    return ResultUtil.ERROR("截止售卖日期不能大于课程到期日期");
                }
            }
        }
        //小班课
        if (course.getCourseTypeKey().equals(CourseType.COURSE_TYPE_SMALL.getKey())) {
            if(ObjectUtils.isEmpty(course.getOpenTime())){
                return ResultUtil.ERROR("请填写开始授课时间");
            }
            if(ObjectUtils.isEmpty(course.getEndTime())){
                return ResultUtil.ERROR("请填写授课结束时间");
            }
            if(ObjectUtils.isEmpty(course.getAudition())){
                return ResultUtil.ERROR("请填写试听视频");
            }
            if(ObjectUtils.isNotEmpty(course.getBuyNum())&&course.getBuyNum()<=0){
                return ResultUtil.ERROR("小班课必须限制课程购买人数");
            }
        }

        //套餐课程的判断
        if(course.getCourseTypeKey().equals(CourseType.COURSE_TYPE_PACKAGE.getKey())){
            if(ObjectUtils.isEmpty(course.getCourseNum()) || course.getCourseNum().intValue()<=0){
                return ResultUtil.ERROR("套餐包含课程数量必须大于0");
            }
            if(course.getUseNum().intValue()>course.getCourseNum().intValue()){
                return ResultUtil.ERROR("套餐下课程最大使用数不能大于包含课程数量");
            }
        }

        if(ObjectUtils.isNotEmpty(course.getStudyLimit()) && course.getStudyLimit().intValue()==3){// 学习限制是--教育限定的
            if(course.getScorePercent().doubleValue()<=0){
                return ResultUtil.ERROR("得分率百分比要大于0");
            }
        }
        if(StringUtils.isEmpty(course.getImageJson())){
            return ResultUtil.ERROR("请上传课程封面");
        }
        //不是套餐课程的判断
      /*  if(!course.getCourseTypeKey().equals(CourseType.COURSE_TYPE_PACKAGE.getKey())){
            if(StringUtils.isEmpty(course.getTeacherIds())){
                return ResultUtil.ERROR("请选择任课讲师");
            }
        }*/
        courseBiz.saveCourse(course);
        return ResultUtil.SUCCESS("课程数据保存成功",course);
    }

    /**
     * 保存描述信息
     * @param course 数据对象
     * @return Map<String,Object>
     */
    @RequestMapping("/saveCourseInfo")
    public Map<String,Object> saveCourseInfo(@RequestBody Course course){
        if(!DataUtil.idIsNotNull(course.getId())){
            return ResultUtil.ERROR("请先创建并保存课程基本信息","NOT_COURSE");
        }
        if(StringUtils.isEmpty(course.getSummary())){
            return ResultUtil.ERROR("请填写课程简介");
        }else{
            if(course.getSummary().trim().length()>255){
                return ResultUtil.ERROR("课程简介内容限制在255个字以内");
            }
        }
        courseBiz.saveCourseInfo(course);
        return ResultUtil.SUCCESS("保存描述信息成功",course);
    }

    /**
     * 更新课程的学习顺序
     * @param course 课程对象
     * @return Map<String,Object>
     */
    @RequestMapping("/updateCourseStudyLimit")
    public Map<String,Object> updateCourseStudyLimit(@RequestBody Course course){
        courseBiz.updateById(course,null);
        return ResultUtil.SUCCESS();
    }

    /**
     * 查询课程详情
     * @param courseId 课程ID
     * @param userId 用户ID
     * @param onLimit 是否有限制，true没有，false有
     * @param setPuyStatus 是否设置购买状态数据
     * @param teacherData 是否设置讲师数据
     * @param subjectData 是否设置专业数据
     * @param sysUserData 是否设置创建人数据
     * @param setProfileData 是否设置扩展数据
     * @param setLiveCourseNodesData  是否设置课程直播节点数据
     * @return Course
     */
    @RequestMapping("/findToBuyCourseById")
    public Course findToBuyCourseById(@RequestParam("courseId") BigDecimal courseId,
                                      @RequestParam("userId") BigDecimal userId,
                                      @RequestParam("onLimit") boolean onLimit,
                                      @RequestParam("setPuyStatus") boolean setPuyStatus,
                                      @RequestParam("teacherData") boolean teacherData,
                                      @RequestParam("subjectData") boolean subjectData,
                                      @RequestParam("sysUserData") boolean sysUserData,
                                      @RequestParam("setProfileData") boolean setProfileData,
                                      @RequestParam("setLiveCourseNodesData") boolean setLiveCourseNodesData){
        Course course = courseBiz.findCourseById(courseId,userId,onLimit,setPuyStatus,teacherData,subjectData,sysUserData,setProfileData,setLiveCourseNodesData);
        //验证课程购买状态
        //售罄和销售时间到了
        //先验证售罄在验证销售到期 如果是下架 和删除 状态 不做验证
        if(course.getStatus()==2 ||  course.getStatus()==3){
            return course;
        }
        if(ObjectUtils.isNotEmpty(course.getInventoryIsFull())&&course.getInventoryIsFull()){
            course.setStatus(4);
        }else if(ObjectUtils.isNotEmpty(course.getLimitDate())){
            if( course.getLimitDay()<=0){
                course.setStatus(5);
            }
        }
        return course;
    }

    /**
     * 查询课程详情
     * @param courseId 课程ID
     * @param userId 用户ID
     * @param onLimit 是否有限制，true没有，false有
     * @param setPuyStatus 是否设置购买状态数据
     * @param teacherData 是否设置讲师数据
     * @param subjectData 是否设置专业数据
     * @param sysUserData 是否设置创建人数据
     * @param setProfileData 是否设置扩展数据
     * @param setPackageCourseData 是否设置套餐数据
     * @return Course
     */
    @RequestMapping("/findToBuyCourseManyById")
    public Course findToBuyCourseManyById(@RequestParam("courseId") BigDecimal courseId,
                                      @RequestParam("userId") BigDecimal userId,
                                      @RequestParam("onLimit") boolean onLimit,
                                      @RequestParam("setPuyStatus") boolean setPuyStatus,
                                      @RequestParam("teacherData") boolean teacherData,
                                      @RequestParam("subjectData") boolean subjectData,
                                      @RequestParam("sysUserData") boolean sysUserData,
                                      @RequestParam("setProfileData") boolean setProfileData,
                                      @RequestParam("setPackageCourseData") boolean setPackageCourseData){
        return courseBiz.findCourseById(courseId,userId,onLimit,setPuyStatus,teacherData,subjectData,sysUserData,setProfileData,setPackageCourseData);
    }

    /**
     * 通过ID，查询课程对象
     * @param courseId 课程ID
     * @param teacherData 是否设置讲师数据
     * @param subjectData 是否设置专业数据
     * @param sysUserData 是否设置创建人数据
     * @return Course
     */
    @RequestMapping("/findCourseById")
    public Course findCourseById(@RequestParam("courseId")BigDecimal courseId,
                                 @RequestParam(value = "teacherData",required = false,defaultValue = "false") boolean teacherData,
                                 @RequestParam(value = "subjectData",required = false,defaultValue = "false") boolean subjectData,
                                 @RequestParam(value = "sysUserData",required = false,defaultValue = "false") boolean sysUserData,
                                 @RequestParam(value = "profileData",required = false,defaultValue = "false") boolean profileData){
        return courseBiz.findCourseById(true,courseId,teacherData,subjectData,sysUserData,profileData,false);
    }

    /**
     * 通过多条件，查询课程数据列表
     * @param condition 课程查询条件
     * @param teacherData 是否设置讲师数据
     * @param subjectData 是否设置专业数据
     * @param sysUserData 是否设置创建人数据
     * @param profileData 是否设置课程扩展数据
     * @return List<Course>
     */
    @RequestMapping("/findCourseList")
    public List<Course> findCourseList(@RequestBody(required = false) CourseCondition condition,
                                       @RequestParam(value = "teacherData",required = false,defaultValue = "false") boolean teacherData,
                                       @RequestParam(value = "subjectData",required = false,defaultValue = "false") boolean subjectData,
                                       @RequestParam(value = "sysUserData",required = false,defaultValue = "false") boolean sysUserData,
                                       @RequestParam(value = "profileData",required = false,defaultValue = "false") boolean profileData){
        return courseBiz.findCourseList(condition,teacherData,subjectData,sysUserData,profileData);
    }

    /**
     * 通过父级专业IDS，查询专业下课程列表
     *
     * @param placeKey 推荐位置
     * @param profileData 是否设置课程扩展数据
     * @return 专业数据列表
     */
    @RequestMapping("/findRecommendCourseList")
    public List<Map<String,Object>> findRecommendCourseList(@RequestParam(value = "placeKey") String placeKey,
                                        @RequestParam(value = "profileData",required = false,defaultValue = "false") boolean profileData,
                                        @RequestParam(value = "teacherData",required = false,defaultValue = "false") boolean teacherData){

        return courseBiz.findRecommendSubjectCourseList(placeKey,profileData,teacherData);
    }

    /**
     * 查询课程Map
     * @param condition 查询条件
     * @return Map<BigDecimal,Course>
     */
    @RequestMapping("/findCourseToMap")
    public Map<BigDecimal,Course> findCourseToMap(@RequestBody CourseCondition condition){
        return courseBiz.findCourseToMapByIds(condition.getIds(),condition.isTeacherData(),condition.isSubjectData(),condition.isSysUserData());
    }

    /**
     * 通过Ids，查询课程数据列表
     * @param courseIds 课程查询条件
     * @param teacherData 是否设置讲师数据
     * @param subjectData 是否设置专业数据
     * @param sysUserData 是否设置创建人数据
     * @return Map<BigDecimal,Course>
     */
    @RequestMapping("/findCourseToMapByIds")
    public Map<BigDecimal,Course> findCourseToMapByIds(@RequestParam("courseIds")String courseIds,
                                                       @RequestParam(value = "teacherData",required = false,defaultValue = "false") boolean teacherData,
                                                       @RequestParam(value = "subjectData",required = false,defaultValue = "false") boolean subjectData,
                                                       @RequestParam(value = "sysUserData",required = false,defaultValue = "false") boolean sysUserData){
        return courseBiz.findCourseToMapByIds(courseIds,teacherData,subjectData,sysUserData);
    }

    /**
     * 查询套餐课程详情
     * @param courseId 课程ID
     * @return Course
     */
    @RequestMapping("/findPackageInfo")
    public Course findPackageInfo(@RequestParam("courseId") BigDecimal courseId){
        return courseBiz.findPackageInfo(courseId);
    }

    /**
     * 分页查询课程列表
     * @param condition 查询条件
     * @param currentPage 页码
     * @param teacherData 是否设置讲师数据
     * @param subjectData 是否设置专业数据
     * @param sysUserData 是否设置创建人数据
     * @param setProfileData 是否设置课程数据扩展数据
     * @param setLiveCourseNodesData 是否设置课程直播节点数据
     * @return PageInfo<Course>
     */
    @RequestMapping("/findWebCoursePage")
    public PageInfo<Course> findWebCoursePage(@RequestBody(required = false) CourseCondition condition,
                                           @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage,
                                           @RequestParam(value = "pageSize",required = false,defaultValue = "10") int pageSize,
                                           @RequestParam(value = "teacherData",required = false,defaultValue = "false") boolean teacherData,
                                           @RequestParam(value = "subjectData",required = false,defaultValue = "false") boolean subjectData,
                                           @RequestParam(value = "sysUserData",required = false,defaultValue = "false") boolean sysUserData,
                                           @RequestParam(value = "setProfileData",required = false,defaultValue = "false") boolean setProfileData,
                                          @RequestParam(value = "setLiveCourseNodesData",required = false,defaultValue = "false") boolean setLiveCourseNodesData){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        return courseBiz.findCoursePage(condition,page,teacherData,subjectData,sysUserData,setProfileData,setLiveCourseNodesData);
    }

    /**
     * 分页查询课程列表
     * @param condition 查询条件
     * @param currentPage 页码
     * @param pageSize 每页显示条数
     * @param teacherData 是否设置讲师数据
     * @param subjectData 是否设置专业数据
     * @param sysUserData 是否设置创建人数据
     * @return
     */
    @RequestMapping("/findCoursePage")
    public PageInfo<Course> findCoursePage(@RequestBody(required = false) CourseCondition condition,
                                           @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage,
                                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
                                           @RequestParam(value = "teacherData",required = false,defaultValue = "false") boolean teacherData,
                                           @RequestParam(value = "subjectData",required = false,defaultValue = "false") boolean subjectData,
                                           @RequestParam(value = "sysUserData",required = false,defaultValue = "false") boolean sysUserData){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        return courseBiz.findCoursePage(condition,page,teacherData,subjectData,sysUserData);
    }

    /**
     * 修改完结状态
     * @param courseIds 课程ID串，多个ID以“,”隔开
     */
    @RequestMapping("/updateContextStatus")
    public Map<String,Object> updateContextStatus(@RequestParam("courseIds") String courseIds){
        courseBiz.updateContextStatus(courseIds);
        return ResultUtil.SUCCESS("课程完结状态设置成功");
    }

    /**
     * 修改课程状态
     * @param courseIds 课程ID串，多个ID以“,”隔开
     * @param status 状态，1下架，2下架，3删除
     * @return Map<String,Object>
     */
    @RequestMapping("/updateCourseStatus")
    public Map<String,Object> updateCourseStatus(@RequestParam("courseIds") String courseIds,
                                                 @RequestParam("status") int status){
        if(StringUtils.isNotEmpty(courseIds)) {
            courseIds = StringUtils.subHeadTailString(courseIds, ",");
            if (StringUtils.isNotEmpty(courseIds)) {
                //先获取课程数据
                List<Course> courseList = courseBiz.find(" id in ("+courseIds+")",null,null);
                if(ObjectUtils.isEmpty(courseList)){
                    return ResultUtil.ERROR("课程数据不存在");
                }
                List<Course> updateList = new ArrayList<>();
                List<Course> deleteList = new ArrayList<>();
                //通过状态分组
                Map<Integer,List<Course>> courseListMap = courseList.stream().collect(Collectors.groupingBy(e->e.getStatus()));
                List<Course> courseList0 = courseListMap.get(0);//获取未上架的
                List<Course> courseList1 = courseListMap.get(1);//获取已上架的
                List<Course> courseList2 = courseListMap.get(2);//获取已下架的
                List<Map<BigDecimal,String>> addList = new ArrayList<>();//累加课程量的讲师数据列表
                List<Map<BigDecimal,String>> subList = new ArrayList<>();//累减课程量的讲师数据列表
                if(status ==1){//上架
                    if(ObjectUtils.isNotEmpty(courseList2)){
                        updateList.addAll(courseList2);
                        this.getUpdateTeacherCourseNumData(courseList2,addList);
                    }
                    if(ObjectUtils.isNotEmpty(courseList0)){//验证未上架的课程
                        //通过课程类型分组
                        Map<Boolean,List<Course>> courseMaps = courseList0.stream().collect(Collectors.groupingBy(e->e.getCourseTypeKey().equals(CourseType.COURSE_TYPE_PACKAGE.getKey())));
                        //得到套餐类型的课程
                        List<Course> packageList = courseMaps.get(true);
                        if(ObjectUtils.isNotEmpty(packageList)){
                            //验证套餐所包含的课程和设置的包含课程量属性是否一致，如果不一致则不能上架
                            String ids = packageList.stream().map(e->e.getId().toString()).collect(Collectors.joining(","));
                            Map<BigDecimal,Integer> numMaps = packageCourseBiz.findPackageCourseNumMapByIds(ids);
                            if(ObjectUtils.isEmpty(numMaps)){
                                return ResultUtil.ERROR("套餐课程未设置课程数据，不可上架");
                            }
                            for(Course course : packageList){
                                Integer num = numMaps.get(course.getId());
                                if(num.intValue()<=0){
                                    return ResultUtil.ERROR("套餐课程【"+course.getCourseName()+"】没有设置课程，不能上架");
                                }
                                if(course.getCourseNum().intValue()!=num.intValue()){
                                    return ResultUtil.ERROR("套餐课程【"+course.getCourseName()+"】要设置"+course.getCourseNum()+"个课程才可上架");
                                }
                            }
                        }
                        //得到非套餐类型的课程
                        List<Course> _courseList = courseMaps.get(false);
                        if(ObjectUtils.isNotEmpty(_courseList)){
                            for(Course course : _courseList){
                                if(StringUtils.isEmpty(course.getImageJson())){
                                    return ResultUtil.ERROR("课程【"+course.getCourseName()+"】未上传图片");
                                }
                                if(StringUtils.isEmpty(course.getSummary())){
                                    return ResultUtil.ERROR("课程【"+course.getCourseName()+"】的课程简介属性未填写");
                                }
                            }
                        }
                        //如果验证通过，则添加到列表中
                        updateList.addAll(courseList0);
                        this.getUpdateTeacherCourseNumData(courseList0,addList);
                    }
                }
                if(status ==2){//下架
                    if(ObjectUtils.isNotEmpty(courseList1)){
                        updateList.addAll(courseList1);//如果有则添加
                        this.getUpdateTeacherCourseNumData(courseList1,subList);
                    }
                }
                if(status==3){//删除
                    //真删除
                    if(ObjectUtils.isNotEmpty(courseList0)) {//如果是上架的课程
                        deleteList.addAll(courseList0);//如果有则添加
                    }
                    //修改状态
                    if(ObjectUtils.isNotEmpty(courseList1)) {//如果是上架的课程
                        updateList.addAll(courseList1);//如果有则添加
                        this.getUpdateTeacherCourseNumData(courseList1,subList);
                    }
                    if(ObjectUtils.isNotEmpty(courseList2)){//如果是下架的课程
                        updateList.addAll(courseList2);//如果有则添加
                        this.getUpdateTeacherCourseNumData(courseList2,subList);
                    }
                }
                //修改课程状态
                courseBiz.updateCourseStatus(updateList,deleteList,status,addList,subList);

            }
        }
        String message = "";
        if(status ==1){
            message = "课程上架成功";
        }
        if(status==2){
            message = "课程下架成功";
        }
        if(status==3){
            message = "课程删除成功";
        }
        return ResultUtil.SUCCESS(message);
    }

    /**
     * 查询H5和App课程首页的精准推荐
     * @param userId 用户ID，用于精准推荐
     * @return Map<String,Object>
     */
    @RequestMapping("/getAppH5RecommendIndex")
    public Map<String,Object> getAppH5RecommendIndex(@RequestParam(value = "userId",required = false,defaultValue = "0") BigDecimal userId){
        Map<String,Object> dataMap = new HashMap<>();
        List<Course> accurateList = null;

        //查询用户对象
        Map<String,Object> userMap = null;
        if(DataUtil.idIsNotNull(userId)){
            userMap = cusUserService.userInfo(userId,false,false,false,false,false,false,false);
        }
        //获取用户ID
        String subjectIds = null;
        if(ObjectUtils.isNotEmpty(userMap)){//用户有专业
            String _subjectIds = (String)userMap.get("subjectIds");
            if(StringUtils.isNotEmpty(_subjectIds)){
                subjectIds = Stream.of(_subjectIds.split(",")) .filter(StringUtils::isNumber).distinct().collect(Collectors.joining(","));
            }
        }
        if(StringUtils.isNotEmpty(subjectIds)){
            //获取精准推荐
            CourseCondition courseCondition = new CourseCondition();
            courseCondition.setSubjectIds(subjectIds);
            courseCondition.setCount(10);
            //不获取已下架的课程
            courseCondition.setStatus(1);
            accurateList = courseBiz.findCourseList(courseCondition,true,false,false,true);
        }
        dataMap.put("accurateList",accurateList);

        Map<String, Object> params = new HashMap<>();
        params.put("placeKey", "ARC");
        //获取课程推荐
        List<Map<String,Object>> _courseList = opGroomDataService.queryGroomDataList(params);
        List<Map<String,Object>> courses = new ArrayList<>();
        if(ObjectUtils.isNotEmpty(_courseList)){
            _courseList.forEach(course->{
                Map<String,Object> dataTypeMap = (Map<String,Object>)course.get("dataTypeMap");
                courses.add(dataTypeMap);
            });
        }
        dataMap.put("groomCourseList",courses);
        return dataMap;
    }



    /**
     * 引导用户去购买课程
     * @param courseId 课程ID
     * @param userId 用户ID
     * @param onLimit 是否有限制，true没有，false有
     * @param setPuyStatus 是否设置购买状态数据
     * @param teacherData 是否设置讲师数据
     * @param subjectData 是否设置专业数据
     * @param sysUserData 是否设置创建人数据
     * @param setProfileData 是否设置扩展数据
     * @param setLiveCourseNodesData  是否设置课程直播节点数据
     * @return Course
     */
    @RequestMapping("/guideBuyCourse")
    public Map<String,Object> guideBuyCourse(@RequestParam("courseId") BigDecimal courseId,
                                      @RequestParam("userId") BigDecimal userId,
                                      @RequestParam("onLimit") boolean onLimit,
                                      @RequestParam("setPuyStatus") boolean setPuyStatus,
                                      @RequestParam("teacherData") boolean teacherData,
                                      @RequestParam("subjectData") boolean subjectData,
                                      @RequestParam("sysUserData") boolean sysUserData,
                                      @RequestParam("setProfileData") boolean setProfileData,
                                      @RequestParam("setLiveCourseNodesData") boolean setLiveCourseNodesData){
        Course course = courseBiz.findCourseById(courseId,userId,onLimit,setPuyStatus,teacherData,subjectData,sysUserData,setProfileData,setLiveCourseNodesData);
        //用户ID
        String message = null;
        //  type 1 为免费课程; 2 为课程购买; 3 为会员课程开通会员
        Integer type = null;
        Map<String, Object>  courseBuyStatus=  course.getCourseBuyStatus();
        //判断该课程的购买方式
        //验证课程是否已购买
        if(!(boolean)courseBuyStatus.get("buy")){
            //验证课程是否为免费播放
            if((boolean)courseBuyStatus.get("freeBuyBut")){
                message = "该课程为免费课程";
                type =1;
            }else if((boolean)courseBuyStatus.get("buyBut")){
                message = "购买课程";
                type =2;
            }else if(!(boolean)courseBuyStatus.get("buyBut")&&(boolean)courseBuyStatus.get("memberBut")){
                message = "该课程为会员课程需要开通会员";
                type =3;
            }
        }

        return ResultUtil.SUCCESS(message,type);
    }

    /**
     * 在用户没有购买课程的情况下，获取给用户的推荐课程
     * @param userId 用户ID
     * @return
     */
    public List<Course> findUserRecommendedCourse(@RequestParam("userId") BigDecimal userId){
        cusUserService.findUserExamSelectSubjectList(userId);
        return null;
    }

    /**
     * 获取更新更新讲师课程数量的前置数据
     * @param courseList 课程列表
     * @param mapList 缓存数据的List
     */
    private void getUpdateTeacherCourseNumData(List<Course> courseList,List<Map<BigDecimal,String>> mapList){
        courseList.stream().filter(e->StringUtils.isNotEmpty(e.getTeacherIds())).forEach(e->{
            Map<BigDecimal,String> map = new HashMap<>();
            map.put(e.getId(),e.getTeacherIds());
            mapList.add(map);
        });
    }

    /**
     * 查询课程总数量数据
     * @return
     */
    @RequestMapping("/queryCourseSumCount")
    public CourseSumCount queryCourseSumCount(){
        return courseBiz.queryCourseSumCount();
    }


    /**
     * 分页查询课程列表
     * @param condition 查询条件
     * @param currentPage 页码
     * @return PageInfo<Course>
     */
    @RequestMapping("/findCoursePracticePage")
    public PageInfo<Course> findCoursePracticePage(@RequestBody(required = false) CourseCondition condition,
                                              @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage,
                                              @RequestParam(value = "pageSize",required = false,defaultValue = "10") int pageSize){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        return courseBiz.findCoursePracticePage(condition,page);
    }

    /**
     * 查询课程详情
     * @return Course
     */
    @RequestMapping("/findByIdCourse")
    public  Course findByIdCourse(@RequestParam("courseId") BigDecimal courseId){
      return courseBiz.findById(courseId);
    }


    /**
     * 计算并获取课程总时长
     * @param courseId
     * @return
     */
    @RequestMapping("/durationSum")
    Map<String, Object> durationSum(@RequestParam("courseId") BigDecimal courseId){
        Course course = courseBiz.durationSum(courseId);
        return ResultUtil.SUCCESS(course);
    }


    /**
     * 修改课程分销推广状态
     * @param courseId 课程id
     * @param allow  状态 1 不可分销  2  开启分销
     * @return
     */
    @RequestMapping("/updateCourseAllow")
    public Map<String, Object> updateCourseAllow(@RequestParam("courseId")BigDecimal courseId,@RequestParam("allow")Integer allow){
        Course course = new Course();
        course.setId(courseId);
        course.setAllow(allow);
        courseBiz.updateById(course, null);
        return ResultUtil.SUCCESS();
    }
}
